package com.huizu.project.apiController;

import cn.hutool.core.collection.CollUtil;
import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.config.TerAspect;
import com.huizu.project.config.UserAspect;
import com.huizu.project.domain.*;
import com.huizu.project.service.IProLdService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import com.huizu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础劳动 Controller
 *
 * @author zhangmx
 * @date 2024-05-10
 */
@Anonymous
@RestController
@RequestMapping("/api/prold")
public class ApiProLdController extends BaseController
{
    @Autowired
    private IProLdService proLdService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 基础劳动列表
     */
    @UserAspect
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProLd proLd,HttpServletRequest request)
    {

        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLd.setUserId(userId);
        startPage();
        List<ProLd> list = proLdService.selectProLdListApi(proLd);
        return getDataTable(list);
    }


    /**
     * 查询通过基础劳动的单子
     */
    @UserAspect
    @PostMapping("/listPass")
    public TableDataInfo listPass(@RequestBody ProLd proLd,HttpServletRequest request)
    {
//        startPage();
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLd.setUserId(userId);
        proLd.setStatus("2");
        List<ProLd> list = proLdService.selectProLdListApi(proLd);
        return getDataTable(list);
    }

    /**
     * 查询驳回的基础劳动单子
     */
    @UserAspect
    @PostMapping("/listReject")
    public TableDataInfo listReject(@RequestBody ProLd proLd,HttpServletRequest request)
    {
//        startPage();
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLd.setUserId(userId);
        proLd.setStatus("3");
        proLd.setBeUserId(userId);
        List<ProLd> list = proLdService.selectProLdListReApi(proLd);
        return getDataTable(list);
    }

    /**
     * 获取
     * @param proLd
     * @param request
     * @return
     */
    @TerAspect
    @PostMapping("/terList")
    public TableDataInfo terList(@RequestBody ProLd proLd, HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id

        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> list = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = list.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proLd.setBjIds(bjs);
        startPage();
        return getDataTable(proLdService.selectProLdListTerApi(proLd));
    }

    @TerAspect
    @PostMapping("/terList/all")
    public TableDataInfo terListAll(@RequestBody ProLd proLd, HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id

        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> list = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = list.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proLd.setBjIds(bjs);
        startPage();
        return getDataTable(proLdService.selectProLdAllListTerApi(proLd));
    }

    /**
     * 教师待审和列表
     * @param proLd
     * @param request
     * @return
     */
    @TerAspect
    @PostMapping("/terListMsg")
    public TableDataInfo terListMsg(@RequestBody ProLd proLd, HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> list = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = list.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proLd.setBjIds(bjs);
        proLd.setStatus("1");
        startPage();
        return getDataTable(proLdService.selectProLdListTerApi(proLd));
    }

    /**
     * 获取宿舍/教/实验劳动详细信息
     */
    @PostMapping(value = "/getById")
    public AjaxResult getInfo(@RequestBody ProLd proLd)
    {
        String id = proLd.getId();
        return success(proLdService.selectProLdById(id));
    }

    /**
     * 新增宿舍/教/实验劳动
     */
    @UserAspect
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProLd proLd, HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLd.setUserId(userId);
        return toAjax(proLdService.insertProLdApi(proLd));
    }

    @UserAspect
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateProLdApi(@RequestBody ProLd proLd,HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLd.setUserId(userId);
        return toAjax(proLdService.updateProLdApi(proLd));
    }


    /**
     * 通过
     */
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody List<ProLd> proLds)
    {
        for (ProLd proLd : proLds) {
            proLdService.pass(proLd);
        }
        return toAjax(1);
    }

    /**
     * 驳回
     */
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody List<ProLd> proLds)
    {
        for (ProLd proLd : proLds) {
            proLdService.reject(proLd);
        }
        return toAjax(1);
    }


    /**
     * 修改宿舍/教/实验劳动
     */
    @PreAuthorize("@ss.hasPermi('project:prold:edit')")
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProLd proLd)
    {
        return toAjax(proLdService.updateProLd(proLd));
    }

    /**
     * 删除宿舍/教/实验劳动
     */
    @PreAuthorize("@ss.hasPermi('project:prold:remove')")
    @Log(title = "基础劳动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proLdService.deleteProLdByIds(ids));
    }
}
